{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "view-in-github"
   },
   "source": [
    "<a href=\"https://colab.research.google.com/github/qgallouedec/panda-gym/blob/colab/PickAndPlace.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "TFFVaHq_7fSe"
   },
   "source": [
    "# `panda-gym` code example\n",
    "\n",
    "## Install `panda-gym`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install panda-gym"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "qE8atPQm870Q"
   },
   "source": [
    "## Interract"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gymnasium as gym\n",
    "import panda_gym\n",
    "\n",
    "env = gym.make(\"PandaPickAndPlace-v3\", render_mode=\"rgb_array\")\n",
    "observation, info = env.reset()\n",
    "\n",
    "images = [env.render()]\n",
    "for _ in range(200):\n",
    "    action = env.action_space.sample()\n",
    "    observation, reward, terminated, truncated, info = env.step(action)\n",
    "    images.append(env.render())\n",
    "\n",
    "    if terminated or truncated:\n",
    "        observation, info = env.reset()\n",
    "        images.append(env.render())\n",
    "\n",
    "env.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Nkl-EtEZ_EQH"
   },
   "source": [
    "## Convert into animation\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install numpngw\n",
    "from numpngw import write_apng\n",
    "\n",
    "write_apng(\"anim.png\", images, delay=40)  # real-time rendering = 40 ms between frames"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "dFadL1ne_NG8"
   },
   "source": [
    "## Show animation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import Image\n",
    "\n",
    "Image(filename=\"anim.png\")"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
